﻿using System;
using System.IO;
using System.Data;
using System.Windows;
using System.Collections;
using System.Windows.Input;
using System.ComponentModel;
using System.Windows.Controls;
using Sinacor.Infra.UI.Common;
using Sinacor.Infra.UI.Controls;
using Sinacor.Infra.UI.Controls.SmartClient;
using System.Collections.Generic;
using Sinacor.Infra.UI.Controls.SmartClient.Validation;
using Sinacor.UI.Bovespa.Ordens.View.SmartClient;
using Sinacor.UI.Bovespa.Clientes.View.SmartClient;
using System.Windows.Threading;
using System.Linq;
using Sinacor.UI.Bovespa.ManutencaoOrdens.Presenter;

namespace Sinacor.UI.Bovespa.ManutencaoOrdens.View.SmartClient
{
    public partial class ManutencaoOrdens : SinacorWindow, IManutencaoOrdens
    {
        #region Event Handlers

        /// <summary>
        /// dtgOrdens - ContextMenuClosing event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dtgOrdens_ContextMenuClosing(object sender, ContextMenuEventArgs e)
        {
            this.dtgOrdens.AllowBeginUpdate = true;
        }

        /// <summary>
        /// dtgOrdens - ContextMenuOpening event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dtgOrdens_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            this.dtgOrdens.AllowBeginUpdate = false;
        }

        private void menuAlterarModoFormulario_Click(object sender, RoutedEventArgs e)
        {
            AlterarModoFormulario();
        }

        /// <summary>
        /// Loaded event - menuAlterarModoFormulario
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuAlterarModoFormulario_Loaded(object sender, RoutedEventArgs e)
        {
            AnalisarPermissaoAlterar(sender as SinacorContextMenuItem);
        }

        /// <summary>
        /// Click event - menuAlterarGrade
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuAlterarGrade_Click(object sender, RoutedEventArgs e)
        {
            dtgOrdens.BeginUpdate();
        }

        /// <summary>
        /// Loaded event - menuAlterarGrade
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuAlterarGrade_Loaded(object sender, RoutedEventArgs e)
        {
            SinacorContextMenuItem contextMenuItem = sender as SinacorContextMenuItem;

            if (PresenterManutencaoOrdens.OrdensAtuais != null)
                contextMenuItem.IsEnabled = false;
            else
                AnalisarPermissaoAlterar(sender as SinacorContextMenuItem);
        }

        /// <summary>
        /// Evento para cancelamento da Ordem
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCancelarOrdem_Click(object sender, RoutedEventArgs e)
        {
            CancelarOrdens();
        }

        /// <summary>
        /// Evento para verificar se habilita ou não o botão de cancelamento da ordem
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCancelarOrdem_Loaded(object sender, RoutedEventArgs e)
        {
            AnalisarPermissaoCancelar(sender as SinacorContextMenuItem);
        }

        /// <summary>
        /// Marcar como casamento
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCasamento_Click(object sender, RoutedEventArgs e)
        {
            MarcarCasamento();
        }

        /// <summary>
        /// Responsável por habilitar ou desabilitar o menu se o grid não contiver registros
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCasamento_Loaded(object sender, RoutedEventArgs e)
        {
            if (DtgDetalhes.Items.Count == 0)
                ((SinacorContextMenuItem)sender).IsEnabled = false;
            else
                ((SinacorContextMenuItem)sender).IsEnabled = true;
        }

        /// <summary>
        /// Click event - menuFecharOrdem
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuFecharOrdem_Click(object sender, RoutedEventArgs e)
        {
            FecharOrdem();
        }

        /// <summary>
        /// Loaded event - menuFecharOrdem
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuFecharOrdem_Loaded(object sender, RoutedEventArgs e)
        {
            (sender as SinacorContextMenuItem).IsEnabled = (PresenterManutencaoOrdens.OrdemAtual != null
                                                            && PresenterManutencaoOrdens.OrdensAtuais == null
                                                            && !(this.dtgOrdens.IsInserting || this.dtgOrdens.IsUpdating));
        }

        private void menuIncluirModoFormulario_Click(object sender, RoutedEventArgs e)
        {
            IncluirModoFormulario();
        }

        /// <summary>
        /// Loaded event - menuIncluirModoFormulario
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuIncluirModoFormulario_Loaded(object sender, RoutedEventArgs e)
        {
            (sender as SinacorContextMenuItem).IsEnabled = !(this.dtgOrdens.IsInserting || this.dtgOrdens.IsUpdating) &&
                                                            this.PresenterManutencaoOrdens.Permissoes.PermiteIncluir;
        }

        /// <summary>
        /// Click event - menuIncluirGrade
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuIncluirGrade_Click(object sender, RoutedEventArgs e)
        {
            //Criar nova linha na GridManutençãoOrdens;
            this.dtgOrdens.BeginInsert();
        }

        /// <summary>
        /// Loaded event - menuIncluirGrade
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuIncluirGrade_Loaded(object sender, RoutedEventArgs e)
        {
            (sender as SinacorContextMenuItem).IsEnabled = !(this.dtgOrdens.IsInserting || this.dtgOrdens.IsUpdating) &&
                                                            this.PresenterManutencaoOrdens.Permissoes.PermiteIncluir;
        }

        private void menuPreBoleto_Loaded(object sender, RoutedEventArgs e)
        {
            (sender as SinacorContextMenuItem).IsEnabled = IsPreBoletoEnabled;
        }

        private void menuPreBoleto_Click(object sender, RoutedEventArgs e)
        {
            PesquisarPreBoletoFormulario();
        }

        private void menuPreBoletoFinanciamentoPOP_Loaded(object sender, RoutedEventArgs e)
        {
            (sender as SinacorContextMenuItem).IsEnabled = btnPreBoletoFinanciamentoPOP.IsEnabled;
        }

        private void menuPreBoletoFinanciamentoPOP_Click(object sender, RoutedEventArgs e)
        {
            ConsultarPreBoletos();
        }

        private void menuObservacoes_Loaded(object sender, RoutedEventArgs e)
        {
            AnalisarPermissaoAlterar(sender as SinacorContextMenuItem);
        }

        private void menuObservacoes_Click(object sender, RoutedEventArgs e)
        {
            AlterarModoFormulario();
            AtribuirFocoCampoObservacao();
        }

        #endregion

        #region Métodos Privados

        private void AnalisarPermissaoCancelar(SinacorContextMenuItem menu)
        {
            if (!PresenterManutencaoOrdens.Permissoes.PermiteCancelar)
                menu.IsEnabled = false;
            else
            {
                AnalisarPermissao(menu);
            }
        }

        private void AnalisarPermissaoAlterar(SinacorContextMenuItem menu)
        {
            if (!PresenterManutencaoOrdens.Permissoes.PermiteAlterar)
                menu.IsEnabled = false;
            else
            {
                AnalisarPermissao(menu);
            }
        }

        private void AnalisarPermissao(SinacorContextMenuItem menu)
        {
            if (this.dtgOrdens.SelectedItems != null
                && dtgOrdens.SelectedItems.Count > 0)
            {
                menu.IsEnabled = !(dtgOrdens.IsInserting || dtgOrdens.IsUpdating);

                if (menu.IsEnabled)
                {
                    foreach (Model.Ordem ordem in dtgOrdens.SelectedItems)
                    {
                        if (ordem.Situacao != null
                            && ordem.Situacao.Value == Model.Enum.SituacaoOrdem.Cancelada)
                        {
                            menu.IsEnabled = false;
                            break;
                        }
                    }
                }
            }
            else
            {
                menu.IsEnabled = false;
            }
        }

        #endregion
    }
}
